#include "shell_color_selector.h"
#include "flash_act.h"
#include "systime.h"
#include "indicator.h"
#include "key.h"

#define SHELL_COLOR_SELECTOR_PROCESS_TIME_S 3

typedef enum
{
	SHELL_COLOR_SELECTOR_INIT,
	SHELL_COLOR_SELECTOR_ENTER,
	SHELL_COLOR_SELECTOR_FINISH,
} shell_color_selector_state_e;

static struct
{
	timestamp_t time;
	shell_color_selector_state_e state;
	uint8_t finish_flag;
	uint16_t shell_color;
	IND_SW ind_sw; // 暂存小灯状态
} shell_color_selector_ctx;

static uint8_t read_shell_color_val(uint16_t *val);
static void write_shell_color_val(shell_color_e val);
static void shell_color_reverse_and_select(uint16_t *color);

void shell_color_selector_init(void)
{
	shell_color_selector_ctx.state = SHELL_COLOR_SELECTOR_INIT;
	shell_color_selector_ctx.finish_flag = FALSE;
	shell_color_selector_ctx.shell_color = SHELL_COLOR_NONE;
	shell_color_selector_ctx.ind_sw = IND_OFF;
}

static void shell_color_reverse_and_select(uint16_t *color)
{
	uint16_t read_color;
	if (!read_shell_color_val(&read_color))
	{ // flash中没有设置，则默认为白色
		*color = SHELL_COLOR_BLACK;
		return;
	}

	if (read_color == SHELL_COLOR_WHITE)
		*color = SHELL_COLOR_BLACK;
	else
		*color = SHELL_COLOR_WHITE;
}

void shell_color_selector_process_task(void)
{
	switch (shell_color_selector_ctx.state)
	{
	case SHELL_COLOR_SELECTOR_INIT:
		if (!shell_color_selector_ctx.finish_flag)
		{
			shell_color_selector_ctx.state = SHELL_COLOR_SELECTOR_ENTER;
		}
		break;
	case SHELL_COLOR_SELECTOR_ENTER:
		set_key_switch(KEY_DISABLE);						  // 按键失能
		shell_color_selector_ctx.ind_sw = get_indicator_sw(); // 暂存小灯当前状态

		shell_color_reverse_and_select(&shell_color_selector_ctx.shell_color);
		if (shell_color_selector_ctx.shell_color == SHELL_COLOR_WHITE)
		{
			set_indicator_sw(IND_WHITE_SHELL); // 快闪烁，指示白色外壳
		}
		else if (shell_color_selector_ctx.shell_color == SHELL_COLOR_BLACK)
		{
			set_indicator_sw(IND_BLACK_SHELL); // 慢闪，指示黑色外壳
		}

		relative_time_add_sec(&shell_color_selector_ctx.time, SHELL_COLOR_SELECTOR_PROCESS_TIME_S);
		write_shell_color_val(shell_color_selector_ctx.shell_color);
		shell_color_selector_ctx.state = SHELL_COLOR_SELECTOR_FINISH;
		break;
	case SHELL_COLOR_SELECTOR_FINISH:
		if (is_relative_time_timeout(&shell_color_selector_ctx.time))
		{
			set_indicator_sw(shell_color_selector_ctx.ind_sw);
			set_key_switch(KEY_ENABLE); // 按键使能
			shell_color_selector_ctx.finish_flag = TRUE;
			shell_color_selector_ctx.state = SHELL_COLOR_SELECTOR_INIT;
		}
		break;
	}
}

uint8_t is_shell_color_selector_finish(void)
{
	return shell_color_selector_ctx.finish_flag;
}

shell_color_e get_shell_color(void)
{
	uint16_t read_color;
	if (!read_shell_color_val(&read_color))
	{ // didn't read the shell color ok
		return SHELL_COLOR_WHITE;
	}
	return read_color;
}

static uint8_t read_shell_color_val(uint16_t *val)
{
	if (Ok == read_shell_color_halfword(val))
	{
		return TRUE;
	}

	return FALSE;
}

static void write_shell_color_val(shell_color_e val)
{
	uint16_t color = (uint16_t)val;
	write_shell_color_halfword(color);
}
